﻿using StoreSQLSugar.DBUtility;
using StoreSQLSugar.Helpers;
using StoreSQLSugar.Models;
using System;
using System.Windows.Forms;

namespace StoreSQLSugar.Forms.UserFrm
{
    public partial class UserAddEditFrm : Form
    {
        private int editId = 0;
        private User editModel = null;
        public UserAddEditFrm(int editId)
        {
            InitializeComponent();
            this.editId = editId;
            Load += UserAddEditFrm_Load;
            btnSave.Click += btnSave_Click;
        }

        private void UserAddEditFrm_Load(object sender, EventArgs e)
        {
            if (editId == 0)
            {
                Text = "添加用户";
                txtAccount.Text = "";
                txtPassword.Text = "";
                cbbUserType.SelectedValue = -1;
                rtbRemark.Text = "";
                btnSave.Text = "添加";
            }
            else
            {
                Text = "修改用户";
                editModel = SqlSugarHelper.Db.Queryable<User>().Single(ut => ut.UserId == editId);
                if (editModel != null)
                {
                    txtAccount.Text = editModel.Account;
                    txtPassword.Text = editModel.Password;
                    cbbUserType.SelectedValue = editModel.UserTypeId;
                    rtbRemark.Text = editModel.Remark;
                }
                btnSave.Text = "编辑";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtAccount.Text))
            {
                MessageBox.Show("账号不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                MessageBox.Show("密码不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (cbbUserType.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show("请选择用户类型！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (editId == 0)
            {
                var model = new User()
                {
                    Account = txtAccount.Text,
                    Password = txtPassword.Text,
                    UserTypeId = int.Parse(cbbUserType.SelectedValue.ToString()),
                    Remark = rtbRemark.Text
                };
                int count = SqlSugarHelper.Db.Queryable<User>().Where(ut => ut.Account == editModel.Account && ut.Status == 0).Count();
                if (count > 0)
                {
                    MessageBox.Show("账号重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int row = SqlSugarHelper.Db.Insertable(model).ExecuteCommand();
                if (row == 0)
                {
                    MessageBox.Show("添加数据有误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                if (editModel == null)
                {
                    MessageBox.Show("修改数据有误，可能非法进入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                editModel.Account = txtAccount.Text;
                editModel.Password = txtPassword.Text;
                editModel.UserTypeId = int.Parse(cbbUserType.SelectedValue.ToString());
                editModel.Remark = rtbRemark.Text;
                editModel.LastUpdateUserId = LoginHelper.User.UserId;
                editModel.LastUpdateTime = DateTime.Now;

                int count = SqlSugarHelper.Db.Queryable<User>().Where(ut => ut.Account == editModel.Account && ut.UserId != editModel.UserId && ut.Status == 0).Count();
                if (count > 0)
                {
                    MessageBox.Show("账号重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int row = SqlSugarHelper.Db.Updateable(editModel).ExecuteCommand();
                if (row == 0)
                {
                    MessageBox.Show("修改数据有误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            DialogResult = DialogResult.OK;
        }
    }
}
